import { ElLoading, ElMessage, ElMessageBox, ElNotification, MessageBoxData } from 'element-plus'
import { LoadingInstance } from 'element-plus/es/components/loading/src/loading'
import { messageDuration } from '/@lib/config'

let loadingInstance: LoadingInstance

export const ModalUtil: any = {}

// 消息提示
ModalUtil.msg = (content: any) => {
  ElMessage.info(content)
}

// 错误消息
ModalUtil.msgError = (content: any) => {
  ElMessage.error(content)
}

// 成功消息
ModalUtil.msgSuccess = (content: any) => {
  ElMessage.success(content)
}
// 警告消息
ModalUtil.msgWarning = (content: any) => {
  ElMessage.warning(content)
}
// 弹出提示
ModalUtil.alert = (content: any) => {
  ElMessageBox.alert(content, '系统提示')
}
// 错误提示
ModalUtil.alertError = (content: any) => {
  ElMessageBox.alert(content, '系统提示', { type: 'error' })
}
// 成功提示
ModalUtil.alertSuccess = (content: any) => {
  ElMessageBox.alert(content, '系统提示', { type: 'success' })
}
// 警告提示
ModalUtil.alertWarning = (content: any) => {
  ElMessageBox.alert(content, '系统提示', { type: 'warning' })
}

ModalUtil.notify = (message: string,
                    title: string,
                    type: 'success' | 'warning' | 'info' | 'error' = 'success',
                    position: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left' = 'top-right',
                    duration: number = messageDuration) => {
  ElNotification({
    title: title || '',
    message,
    type: type || 'success',
    duration,
    position: position || 'top-right',
  })
}
// 通知提示
ModalUtil.notifyInfo = (content: any) => {
  ElNotification.info(content)
}
// 错误通知
ModalUtil.notifyError = (content: any) => {
  ElNotification.error(content)
}
// 成功通知
ModalUtil.notifySuccess = (content: any) => {
  ElNotification.success(content)
}
// 警告通知
ModalUtil.notifyWarning = (content: any) => {
  ElNotification.warning(content)
}
// 确认窗体
ModalUtil.confirm = (content: any): Promise<MessageBoxData> => {
  return ElMessageBox.confirm(content, '系统提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
}
// 提交内容
ModalUtil.prompt = (content: any) => {
  return ElMessageBox.prompt(content, '系统提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
}

// 打开遮罩层
ModalUtil.loading = (content: string) => {
  loadingInstance = ElLoading.service({
    lock: true,
    text: content,
    background: 'rgba(0, 0, 0, 0.7)',
  })
}
// 关闭遮罩层
ModalUtil.closeLoading = () => {
  loadingInstance.close()
}
